<template>
	<view class="uploadImageBox">
		<view style="display: flex;flex-wrap: wrap;">
			<view v-for="(item,index) in imageList" :key="index" class="imageAddBox" :class="{w_h80:page=='home'}">
				<view v-if="!item.upload" class="uploadBox">
					<view />
					<view>
						上传中
					</view>
				</view>
				<image :src="item.url" @click="imageShow=true"  mode="aspectFill"/>
				<template v-if="page!='home'">
					<view class="del" v-if="item.upload" @click.stop="del(item)">
						<view>
							<u-icon name="close" color="#fff" size="20rpx"></u-icon>
						</view>
					</view>
					<view v-if="item.upload" class="success">
						<view>
							<u-icon name="checkmark" color="#fff" size="20rpx"></u-icon>
						</view>
					</view>
				</template>

			</view>
			<view v-if="!imageList.length" class="imageAddBox" @click="imageShow=true">
				<u-icon name="camera-fill" color="#D3D4D6" size="26"></u-icon>
			</view>
		</view>
		<root-portal>
			<u-popup :show="imageShow" :round="10" @close="close">
				<view class="popupTitle">
					<u-icon name="close" @click="close"></u-icon>
					<view>
						图片选择
					</view>
					<view @click="upload">
						上传
					</view>
				</view>
				<view style="height: 60vh;">
					<z-paging :auto='false' ref="paging" style="height: 100%;" :fixed="false" v-model="data"
						@query="query" :default-page-no="1" :default-page-size="20">
						<view class="imageBox">
							<view class="imageItem" v-for="item in data" :key="item.url" @click="imageItem=item">
								<view class="radio" :class='{active:item.url==imageItem.url}'>
									<u-icon name='checkmark' color='#fff' size='30rpx' />
								</view>
								<image :src="item.url" mode="aspectFill" />
							</view>
						</view>
					</z-paging>
				</view>
				<view style="margin: 0 40rpx;">
					<u-button @click='slecetImage' color="#3975c6" v-if="imageItem.url">确认</u-button>
					<u-button :disabled="true" v-else>确认</u-button>
				</view>
			</u-popup>
		</root-portal>
	</view>
</template>

<script>
	export default {
		props: ['imageUrl', 'page'],
		data() {
			return {
				imageList: [

				],
				imageShow: false,
				data: [],
				imageItem: {}
			}
		},
		mounted() {
			console.log(this.imageUrl)
			if (this.imageUrl.length) {
				this.imageList = this.imageUrl
			}
		},
		watch: {
			imageShow() {
				if (this.imageShow || this.data.length == 0) {
					this.$refs.paging.reload()
				}
				this.imageItem = {}
			}
		},
		methods: {
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中 ...'
				})
				this.$api_company.Image.List(obj).then(res => {
					uni.hideLoading()
					this.$refs.paging.complete(res.data.images.data)
				}).catch(err => {
					uni.$u.toast(err.message)
					uni.hideLoading()
				})
			},
			slecetImage() {
				this.imageList[0] = {
					url: this.imageItem.url,
					coverUrl: this.imageItem.url,
					id: this.imageItem.id,
					thumb_small: this.imageItem.url_small,
					upload: true
				}
				this.$emit('changeImageList', this.imageList)
				this.imageShow = false
			},
			upload() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						this.imageShow = false
						res.tempFilePaths.forEach((item, index) => {
							this.imageList[0] = {
								url: item,
								coverUrl: item,
								id: null,
								upload: false
							}
						})
						this.$emit('changeImageList', this.imageList)
						uni.uploadFile({
							url: this.$config.url + '/company/api/media/image/upload',
							filePath: this.imageList[0].url,
							name: 'file',
							header: {
								'Content-Type': 'application/json;charset=UTF-8',
								'Authorization': "Bearer " + uni.getStorageSync('token')
							},
							success: (res) => {
								const data = JSON.parse(res.data).data.image
								this.$set(this.imageList, 0, {
									id: data.id,
									url: data.url,
									coverUrl: data.url,
									upload: true,
									thumb_small: data.url_small,
								})
								this.$emit('changeImageList', this.imageList)
							},
							fail: (err) => {
								console.log(err)
							}
						});
					},
				})
			},
			del(item) {
				this.imageList = this.imageList.filter(image => image.id != item.id)
				this.$emit('changeImageList', this.imageList)
			},
			close() {
				this.imageShow = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.w_h80 {
		width: 80rpx !important;
		height: 80rpx !important;
		border-radius: 100% !important;
		overflow: hidden;
		margin: 0 !important;
	}

	.uploadImageBox {
		display: flex;
	}

	.imageAddBox {
		width: 160rpx;
		height: 160rpx;
		background-color: #f4f5f7;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		margin-right: 20rpx;
		margin-bottom: 8px;

		.success {
			position: absolute;
			bottom: 0;
			right: 0;
			display: flex;
			flex-direction: row;
			border-style: solid;
			border-top-color: transparent;
			border-left-color: transparent;
			border-bottom-color: #5ac725;
			border-right-color: #5ac725;
			border-width: 9px;
			align-items: center;
			justify-content: center;

			>view {
				position: absolute;
				-webkit-transform: scale(0.7);
				transform: scale(0.7);
				bottom: -10px;
				right: -10px;
			}
		}

		.del {
			position: absolute;
			top: 0;
			right: 0;
			background-color: #373737;
			height: 14px;
			width: 14px;
			display: flex;
			flex-direction: row;
			border-bottom-left-radius: 100px;
			align-items: center;
			justify-content: center;
			z-index: 3;

			>view {
				position: absolute;
				-webkit-transform: scale(0.7);
				transform: scale(0.7);
				top: 0px;
				right: 0px;
			}
		}

		>image {
			width: 100%;
			height: 100%;
		}

		.uploadBox {
			position: absolute;
			background-color: rgba(0, 0, 0, 0.3);
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 20rpx;
			color: #fff;
			width: 100%;
			height: 100%;

			>view:first-child {
				border-radius: 100%;
				border: 1rpx solid #fff;
				width: 40rpx;
				height: 40rpx;
				margin-bottom: 10rpx;
			}
		}
	}

	.imageBox {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 40rpx;
		flex-wrap: wrap;

		&::after {
			content: '';
			width: 31%;
			height: 300rpx;
		}

		.imageItem {
			width: 31%;
			border-radius: 8rpx;
			overflow: hidden;
			box-shadow: 0 0 10rpx #ddd;
			margin-bottom: 20rpx;
			height: 300rpx;
			position: relative;
		}

		image {
			width: 100%;
			height: 100%;
		}
	}

	.radio {
		background-color: #ffffff;
		border-color: #c8c9cc;
		width: 18px;
		height: 18px;
		border-radius: 100%;
		box-sizing: border-box;
		transition-property: border-color, background-color, color;
		transition-duration: 0.2s;
		color: #606266;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		color: transparent;
		text-align: center;
		margin-right: 6px;
		font-size: 20px;
		border-width: 1px;
		border-color: #c8c9cc;
		border-style: solid;
		display: flex;
		flex-direction: column;
		flex-shrink: 0;
		flex-grow: 0;
		flex-basis: auto;
		align-items: stretch;
		align-content: flex-start;
		position: absolute;
		right: 5rpx;
		top: 10rpx;
		justify-content: center;

	}

	.active {
		background-color: #2979ff !important;
		border-color: #2979ff !important;
	}
</style>